module.exports = {
    currentPage: null,
    currentPageOptions: {},
    onLoad: function(t, e) {
        var o = this;
        this.currentPage = t
        this.currentPageOptions = e
        this.setUserInfo()
        this.setBarTitle()
        "function" == typeof t.onSelfLoad && t.onSelfLoad(e)
        o._setFormIdSubmit()
        getApp().args.core.setStorageSync("last_page_options", e)
        getApp().args.shareSendCoupon = function(e) {o.shareSendCoupon(e)}
        t.navigatorClick = function(e) {o.navigatorClick(e, t)}
        t.setData({
            __platform: getApp().args.platform,
            _api_root:getApp().args.api.siteinfo.siteroot,
            //_api_live:getApp().args.api.siteinfo.sitelive
        })
        t.showToast || (t.showToast = function(e) {o.showToast(e)})
        t.setTimeList || (t.setTimeList = function(e) {return o.setTimeList(e)})
        t.showLoading || (t.showLoading = function(e) {o.showLoading(e)})
        t.hideLoading || (t.hideLoading = function(e) {o.hideLoading(e)})
        t.modalConfirm || (t.modalConfirm = function(e) {o.modalConfirm(e)})
        t.modalClose || (t.modalClose = function(e) {o.modalClose(e)})
        t.modalShow || (t.modalShow = function(e) {o.modalShow(e)})
        t.myLogin || (t.myLogin = function() {o.myLogin()})
        t.getUserProfile || (t.getUserProfile = function(e) {o.getUserProfile(e)})
        t.bindParent || (t.bindParent = function(e) {o.bindParent(e)})
        t.closeCouponBox || (t.closeCouponBox = function(e) {o.closeCouponBox(e)})
        t.clearInfo || (t.clearInfo = function (e) {o.clearInfo(e)})
        this.getLogin(t)
    },
    getLogin(t){
        t.setData({getapp:getApp().args})
        var token = wx.getStorageSync('ACCESS_TOKEN');
        var _api_live = getApp().args.api.siteinfo.sitelive;
        // 统计 和 卡bug
        token || getApp().args.request({
            url: _api_live + 'action/guanfangShow',
            data:{storeId:getApp().args.api.siteinfo.acid, time:'2021-04-19'},
            success(e){
                console.log(e)
                t.setData({showLogin: 1 != e.showguanfang})
            }
        })
    },
    clearInfo:function(){
        this.currentPage.setData({
            showLogin:true
        })
    },
    onReady: function(e) {
        this.currentPage = e;
    },
    onShow: function(e) {
        this.currentPage = e, getApp().args.orderPay.init(e, getApp().args);
    },
    onHide: function(e) {
        this.currentPage = e;
    },
    onUnload: function(e) {
        this.currentPage = e;
    },
    onPullDownRefresh: function(e) {
        this.currentPage = e;
    },
    onReachBottom: function(e) {
        this.currentPage = e;
    },
    onShareAppMessage: function(e) {
        this.currentPage = e, getApp().args.shareSendCoupon(e);
    },
    formSubmit_collect: function(e) {
        e.detail.formId;
        console.log("formSubmit_collect--\x3e", e);
    },
    setUserInfo: function() {
        var e = this.currentPage, t = getApp().args.getUser();
        t && e.setData({
            __user_info: t
        });
    },
    showToast: function(e) {
        var t = this.currentPage, o = e.duration || 2500, n = e.title || "", a = (e.success, 
        e.fail, e.complete || null);
        t._toast_timer && clearTimeout(t._toast_timer), t.setData({
            _toast: {
                title: n
            }
        }), t._toast_timer = setTimeout(function() {
            var e = t.data._toast;
            e.hide = !0, t.setData({
                _toast: e
            }), "function" == typeof a && a();
        }, o);
    },
    showLoading: function(e) {
        var t = t;
        t.setData({
            _loading: !0
        });
    },
    hideLoading: function(e) {
        this.currentPage.setData({
            _loading: !1
        });
    },
    setTimeList: function(e) {
        function t(e) {
            return e <= 0 && (e = 0), e < 10 ? "0" + e : e;
        }
        var o = "00", n = "00", a = "00", i = 0;
        return 86400 <= e && (i = parseInt(e / 86400), e %= 86400), e < 86400 && (a = parseInt(e / 3600), 
        e %= 3600), e < 3600 && (n = parseInt(e / 60), e %= 60), e < 60 && (o = e), {
            d: i,
            h: t(a),
            m: t(n),
            s: t(o)
        };
    },
    setBarTitle: function(e) {
        var t = this.currentPage.route, o = getApp().args.core.getStorageSync(getApp().args.const.WX_BAR_TITLE);
        for (var n in o) o[n].url === t && getApp().args.core.setNavigationBarTitle({
            title: o[n].title
        });
    },
    navigatorClick: function(e, t) {
        console.log("old-view")
        var o = e.currentTarget.dataset.open_type;
        if ("redirect" == o) return !0;
        if ("wxapp" != o) {
            if ("tel" == o) {
                var n = e.currentTarget.dataset.tel;
                getApp().args.core.makePhoneCall({
                    phoneNumber: n
                });
            }
            return !1;
        }
    },
    shareSendCoupon: function(o) {
        var n = getApp().args;
        n.core.showLoading({
            mask: !0
        }), o.hideGetCoupon || (o.hideGetCoupon = function(e) {
            var t = e.currentTarget.dataset.url || !1;
            o.setData({
                get_coupon_list: null
            }), t && n.core.navigateTo({
                url: t
            });
        }), n.request({
            url: n.api.coupon.share_send,
            success: function(e) {
                0 == e.code && o.setData({
                    get_coupon_list: e.data.list
                });
            },
            complete: function() {
                n.core.hideLoading();
            }
        });
    },
    _setFormIdSubmit: function(e) {
        var s = this.currentPage;
        s._formIdSubmit || (s._formIdSubmit = function(e) {
            console.log("_formIdSubmit e --\x3e", e);
            var t = e.currentTarget.dataset, o = e.detail.formId, n = t.bind || null, a = t.type || null, i = t.url || null, r = getApp().args.core.getStorageSync(getApp().args.const.FORM_ID_LIST);
            switch (r && r.length || (r = []), r.push({
                time: getApp().args.helper.time(),
                form_id: o
            }), getApp().args.core.setStorageSync(getApp().args.const.FORM_ID_LIST, r), console.log("self[bindtap]--\x3e", s[n]), 
            s[n] && "function" == typeof s[n] && s[n](e), a) {
              case "navigate":
                i && getApp().args.core.navigateTo({
                    url: i
                });
                break;

              case "redirect":
                i && getApp().args.core.redirectTo({
                    url: i
                });
                break;

              case "switchTab":
                i && getApp().args.core.switchTab({
                    url: i
                });
                break;

              case "reLaunch":
                i && getApp().args.core.reLaunch({
                    url: i
                });
                break;

              case "navigateBack":
                i && getApp().args.core.navigateBack({
                    url: i
                });
            }
        });
    },
    modalClose: function(e) {
        this.currentPage.setData({
            modal_show: !1
        }), console.log("你点击了关闭按钮");
    },
    modalConfirm: function(e) {
        this.currentPage.setData({
            modal_show: !1
        }), console.log("你点击了确定按钮");
    },
    modalShow: function(e) {
        this.currentPage.setData({
            modal_show: !0
        }), console.log("点击会弹出弹框");
    },
    /**
     * 授权登录 使用 getUserProfile 方法获取用户信息
     */
    getUserProfile: function (o) {
      let _this = this;      
      wx.getUserProfile({
        desc: '业务需要',
        success: res => {
          // 存储当前用户信息
          let e = res;
          getApp().getUserProfile(e, () => {
            // 后退回原页面
            //_this.onNavigateBack(1);

            // 跳转回当前页面
            var pages = getCurrentPages()    //获取加载的页面
            var currentPage = pages[pages.length-1]    //获取当前页面的对象
            var url = currentPage.route    //当前页面url
            var options = currentPage.options    //如果要获取url中所带的参数可以查看options            
            //拼接url的参数
            var urlWithArgs = url + '?'
            for(var key in options){
                var value = options[key]
                urlWithArgs += key + '=' + value + '&'
            }
            urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length-1)
            getApp().args.core.redirectTo({
                url: "/" + urlWithArgs
            });

          });
        },
        // 拒绝后 按钮禁用状态 true
        fail: function () {
         
        }
      })
    },
    /**
     * 授权成功 跳转回原页面
     */
    onNavigateBack(delta) {
      wx.navigateBack({
        delta: Number(delta || 1)
      });
    },
    // 获取用户信息 旧版本方法 LCL # 已废弃
    getUserInfo: function(o) {
        console.log('login-2')
          var n = this;
          "getUserInfo:ok" == o.detail.errMsg && getApp().args.core.login({
              success: function(e) {
                  var t = e.code;
                  n.unionLogin({
                      wxapp_id: getApp().args.api.siteinfo.uniacid,
                      code: t,
                      user_info: o.detail.rawData,
                      encrypted_data: o.detail.encryptedData,
                      iv: o.detail.iv,
                      signature: o.detail.signature
                  });
              },
              fail: function(e) {}
          }) //: n.clearInfo()
      },
    unionLogin: function(e) {
        var o = this.currentPage, n = this;
        getApp().args.core.showLoading({
            title: "正在登录",
            mask: !0
        }), getApp().args.request({
            url: getApp().args.api.passport.login,
            method: "POST",
            data: e,
            success: function(e) {
                console.log(e);
                if (0 == e.code) {
                    o.setData({
                        __user_info: e.data
                    }), getApp().args.setUser(e.data), getApp().args.core.setStorageSync(getApp().args.const.ACCESS_TOKEN, e.data.access_token),
                      //n.getAppUserInfo(), getApp().args.trigger.run(getApp().args.trigger.events.login);
                    n.getAppUserInfo();
                    var t = getApp().args.core.getStorageSync(getApp().args.const.STORE);
                    e.data.binding || !t.option.phone_auth || t.option.phone_auth && 0 == t.option.phone_auth ? getApp().args.core.redirectTo({
                        url: "/" + o.route + "?" + getApp().args.helper.objectToUrlParams(o.options)
                    }) : ("undefined" == typeof wx && getApp().args.core.redirectTo({
                        url: "/" + o.route + "?" + getApp().args.helper.objectToUrlParams(o.options)
                    }), n.setPhone(), n.setUserInfoShowFalse());
                } else getApp().args.login_complete = !1, getApp().args.core.showModal({
                    title: "提示",
                    content: e.msg,
                    showCancel: !1
                });
            },
            complete: function() {
                getApp().args.core.hideLoading();
            }
        });
    },
    setUserInfoShow: function() {
        var e = this.currentPage;
        "wx" == getApp().args.platform ? e.setData({
            user_info_show: !0
        }) : this.myLogin();
    },
    setPhone: function() {
        var e = this.currentPage;
        "undefined" == typeof my && e.setData({
            user_bind_show: !0
        });
    },
    setUserInfoShowFalse: function() {
        this.currentPage.setData({
            user_info_show: !1
        });
    },
    closeCouponBox: function(e) {
        this.currentPage.setData({
            get_coupon_list: ""
        });
    },
    getAppUserInfo(){
        /*var token = wx.getStorageSync('ACCESS_TOKEN'),
          customerId = wx.getStorageSync('customerId');
        customerId || ( token && getApp().args.request({
            url:'https://........./action/getMobileAndCustomerIdByToken',
            data:{
                storeId:getApp().args.api.siteinfo.acid,
                userToken: wx.getStorageSync('ACCESS_TOKEN')
            },
            success(e){
                e.result == 0 && (
                  getApp().args.customerId = e.data.customerId,
                  wx.setStorageSync('appUserInfo',e.data),
                    wx.setStorageSync('customerId',e.data.customerId)
                )
            }
        }))*/
    }
};